package com.stylefeng.guns.modular.manager.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.manager.service.IManagerSupplierService;
import com.stylefeng.guns.modular.system.model.ManagerSupplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 供应商管理控制器
 *
 * @author fengshuonan
 * @Date 2018-09-12 16:07:49
 */
@Controller
@RequestMapping("/managerSupplier")
public class ManagerSupplierController extends BaseController {

    private String PREFIX = "/manager/managerSupplier/";

    @Autowired
    private IManagerSupplierService managerSupplierService;

    /**
     * 跳转到供应商管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "managerSupplier.html";
    }

    /**
     * 跳转到添加供应商管理
     */
    @RequestMapping("/managerSupplier_add")
    public String managerSupplierAdd() {
        return PREFIX + "managerSupplier_add.html";
    }

    /**
     * 跳转到修改供应商管理
     */
    @RequestMapping("/managerSupplier_update/{managerSupplierId}")
    public String managerSupplierUpdate(@PathVariable Integer managerSupplierId, Model model) {
        ManagerSupplier managerSupplier = managerSupplierService.selectById(managerSupplierId);
        model.addAttribute("item", managerSupplier);
        LogObjectHolder.me().set(managerSupplier);
        return PREFIX + "managerSupplier_edit.html";
    }

    /**
     * 获取供应商管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(ManagerSupplier managerSupplier, Model model) {
        EntityWrapper<ManagerSupplier> entityWrapper = new EntityWrapper<>(managerSupplier);
        entityWrapper.like("address", managerSupplier.getAddress());
        entityWrapper.like("supplier_name", managerSupplier.getSupplierName());
        model.addAttribute("tagBody", managerSupplier);
        List<ManagerSupplier> result = managerSupplierService.selectWithDataScope(entityWrapper);
        return result;
    }

    /**
     * 新增供应商管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ManagerSupplier managerSupplier) {
        String createBy = Objects.requireNonNull(ShiroKit.getUser()).getAccount();
        managerSupplier.setCreateBy(createBy);
        managerSupplier.setUpdateBy(createBy);
        managerSupplierService.insert(managerSupplier);
        return SUCCESS_TIP;
    }

    /**
     * 删除供应商管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer managerSupplierId) {
        managerSupplierService.deleteById(Long.valueOf(managerSupplierId));
        return SUCCESS_TIP;
    }

    /**
     * 修改供应商管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ManagerSupplier managerSupplier) {
        managerSupplier.setUpdateDate(new Date());
        managerSupplierService.updateById(managerSupplier);
        return SUCCESS_TIP;
    }

    /**
     * 供应商管理详情
     */
    @RequestMapping(value = "/detail/{managerSupplierId}")
    @ResponseBody
    public Object detail(@PathVariable("managerSupplierId") Integer managerSupplierId) {
        return managerSupplierService.selectById(managerSupplierId);
    }
}
